<?php

namespace App\Models\Manufacturer;

use App\Models\Products\Product;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * Class ManufacturerDescription
 * @package App\Models\Manufacturer
 *
 * @method static self filterProducts(?array $product_ids)
 * @method static self filterManufacturerDescriptionToStore($store_id)
 */

class ManufacturerDescription extends Model
{
    protected $table = 'manufacturer_description';
    protected $guarded = [];
    protected $primaryKey = 'manufacturer_description_id';

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function manufacturerDescriptionToStories()
    {
        return $this->hasMany(ManufacturerDescriptionToStore::class, 'manufacturer_description_id', 'manufacturer_description_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function products()
    {
        return $this->hasMany(Product::class, 'manufacturer_id', 'manufacturer_id');
    }

    /**
     * @used-by filterProducts
     *
     * @param Builder $query
     * @param array $product_ids
     *
     * @return Builder
     */
    public function scopeFilterProducts(Builder $query, ?array $product_ids)
    {
        if (! $product_ids) return $query;

        return $query->whereHas('products', function(Builder $query) use($product_ids){
            return $query->whereIn('product_id', $product_ids);
        });
    }

    /**
     *  @used-by filterManufacturerDescriptionToStore
     *
     * @param Builder $query
     * @param $store_id
     * @return Builder
     */
    public function scopeFilterManufacturerDescriptionToStore(Builder $query, $store_id)
    {
        return $query->whereHas('manufacturerDescriptionToStories', function(Builder $query) use($store_id){
            return $query->where('store_id', $store_id);
        });
    }
}
